/**
 * 定义站点地图模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.sw.bcDeviceMonitorParam.bcDeviceMonitorParamManager', ['water.entity.bcDeviceMonitorParam','water.entity.bcMonitorParamDict']);
		
	module.controller('bcDeviceMonitorParamManagerController',function($scope,$state,$stateParams,$bcDeviceMonitorParamService,$bcMonitorParamDictService){
		
		/**
		 * 设备监测项列表数据
		 */
		$scope.paramList = [];
		
		/**
		 * 监测项字典数据
		 */
		$scope.paramDict = [];
		
		/**
		 * 传过来的设备ID
		 */
		$scope.devicceId = $stateParams.deviceId;
		
		/**
		 * 添加参数按钮点击事件
		 */
		$scope.onAddClick = function(){
			$scope.paramList.push({
				DEVICE_ID:$scope.devicceId
			});
		}
		
		/**
		 * 保存按钮点击事件
		 */
		$scope.onCreateClick = function(model){
			$bcDeviceMonitorParamService.create(model).then(function(){
				alert("新增成功");
				$scope.getList($scope.devicceId);
			}).catch(function(msg){
            	alert(msg);
            });
			
		}
		
		/**
		 * 删除按钮点击事件
		 */
		$scope.onDeleteClick = function(model){
			$bcDeviceMonitorParamService.deleteModel(model).then(function(){
				$scope.getList($scope.devicceId);
			});
			
		}
		
		/**
		 * 保存按钮点击事件
		 */
		$scope.onUpdateClick = function(model){
			$bcDeviceMonitorParamService.update(model).then(function(){
				alert("保存成功");
				$scope.getList($scope.devicceId);
			});
		}
		
		/**
		 * 返回按钮点击事件
		 */
		$scope.onGoBackClick = function(){
			$state.go("bcDevice");
		}
		
		/**
		 * 获取仪器关联的监测项
		 */
		$scope.getList = function(devicceId){
			$bcDeviceMonitorParamService.getListByDevice(devicceId).then(function(data){
				if(data == null){
					return;
				}
				$scope.paramList = data;
			});
		}

		/**
		 * 获取监测项字典数据
		 */
		$scope.getParamDict = function(){
			$bcMonitorParamDictService.getList().then(function(data){
				
				var params = [];
				
				for(var i=0;i<data.length;i++){
					var item = data[i];
					params.push({
						k:item.PARAM_CODE_BUS,
						v:item.PARAM_NAME
					});
				}
				$scope.paramDict = params;
				
			});
		}
		
		$scope.init = function(){
			$scope.getParamDict();
			$scope.getList($scope.devicceId);
		}

		$scope.init();
	});
	
}(window, angular));


